<?php
	include_once "settings.php";
	include_once "{$_GLOBAL['reunionhome']}/core/ReunionSQL.php";

	// do nothing at the moment...
	// id=elements_id&value=user_edited_content

	$reunionDB = new ReunionSQL();

	$id		= getpost("id");
	$value	= getpost("value");
	list($action, $id) = explode("_", $id);

	switch($action)
	{
		case "update" :
			// update the value in the database...

			$pattern = '/(wk|list)([0-9]+)(date|event|name|priority|week)/';
			preg_match($pattern, $id, $matches);

			echo updatefield($matches, $value);
		break;
		case "info"	:
			// get the data in form of JSON for the table...
			switch($id)
			{
				case "wk":
					$output = generateJSONweeks($reunionDB->getallweeks());
				break;
				case "list":
					$output = generateJSONparticipants($reunionDB->getallparticipants());
				break;
				case "wklistopts":
					$output = generateJSONweeklist();
				break;
				case "calendar":
					$output = generateJSONcalendar();
				break;
			}

			echo $output;
		break;
		case "add" :
			if($id == "newweek")
			{
				// add week to database...
				$reunionDB->addWeek($value);

				echo "Add another week...";
				break;
			}

			// add person to database ...
			$reunionDB->addparticipant($value);

			echo "Add another person...";
		break;
		case "remove" :
			if(strpos($id, "list") === false)
			{
				// remove the wk number line
				$reunionDB->deleteweek(str_replace("wk", "", $id));
				break;
			}

			// remove the list number line
			$reunionDB->deleteparticipant(str_replace("list", "", $id));
		break;
		case "updateln" :
			$wid = getpost("week");
			$do = getpost("order");

			// update the line in the database...
			$reunionDB->setweek($wid, array("int_DisplayOrder" => $do));
		break;
	}

	function getpost($id) {
		return isset($_REQUEST[$id]) ? $_REQUEST[$id] : "";
	}

	function generateJSONweeks($wks) {
		//EXAMPLE: { weeks : [ { id : "1", date : "Jun 23 - Jun 25", event : "No Events", order : "1" } ] }

		$output = "{ weeks : [ ";
		foreach($wks as $wk)
		{
			$wkoutput = "{ id:\"{$wk->WeekID}\", date:\"{$wk->Date}\", event:\"{$wk->Event}\", order: \"{$wk->DisplayOrder}\" }, ";
			$output .= $wkoutput;
		}
		$output .= "] }";
		$output = str_replace("}, ] }", "} ] }", $output);

		return $output;
	}

	function generateJSONparticipants($pars) {
		//EXAMPLE: { participants : [ { id : "1", name : "Paul Borrego", priority : "1", unavailable : "Week 1, Week 2" } ] }
		global $reunionDB;

		$output = "{ participants : [ ";
		foreach($pars as $par)
		{
			$uas = $reunionDB->getunavailability($par->ParticipantID);
			$unavailable = "";
			$uavals = "";

			foreach($uas as $ua)
			{
				$pwk = $reunionDB->getweek($ua->WeekID);
				$unavailable .= "{$pwk->Date}, ";
				$uavals .= "{$pwk->WeekID},";
			}
			$unavailable = str_replace(', ;', "", $unavailable . ';');
			$uavals = str_replace(',;', "", $uavals . ';');
			$unavailable = $unavailable == ";" ? "No Weeks" : $unavailable;
			$uavals = $uavals == ";" ? "No Weeks" : $uavals;

			$wkoutput = "{ id:\"{$par->ParticipantID}\", name:\"{$par->Name}\", priority:\"{$par->Priority}\", unavailable:\"{$unavailable}\", uavals:\"{$uavals}\" }, ";
			$output .= $wkoutput;
		}
		$output .= "] }";
		$output = str_replace("}, ] }", "} ] }", $output);

		return $output;
	}

	function generateJSONweeklist() {
		// EXAMPLE { "Week 1" : "Week 1", "Week 2" : "Week 2" }
		global $reunionDB;

		$wks = $reunionDB->getallweeks();

		$output = "{ \"No Weeks\" : \"No Weeks\", ";
		foreach($wks as $wk)
		{
			$output .= "\"{$wk->WeekID}\" : \"{$wk->Date}\", ";
		}
		$output .= "}";
		$output = str_replace('", }', '" }', $output);

		return $output;

	}

	function updatefield($info, $val) {

		// Possible updates...
		// wk1date
		// wk1event
		// wk1order
		// list1name
		// list1priority
		// list1week

		global $reunionDB;
		$wkoptmap = array('date'=>'str_Date', 'event'=>'str_Event', 'order'=>'int_DisplayOrder');
		$listoptmap = array('name'=>'str_Name', 'priority'=>'int_Priority');
		list($search, $type, $pid, $field) = $info;

		switch($type)
		{
			case "wk":
				$reunionDB->setweek($pid, array($wkoptmap[$field] => $val));
			break;
			case "list":
				if($field == "week")
				{
					if($val == "No Weeks")
					{
						$reunionDB->clearunavailability($pid);
						break;
					}

					$reunionDB->clearunavailability($pid);

					$wks = explode(",", $val);
					$newval = "";
					foreach($wks as $wk)
					{
						$mywk = $reunionDB->getweek($wk);
						$newval .= "{$mywk->Date}, ";
						$reunionDB->addunavailability($pid, $wk);
					}
					$val = str_replace(", ;", "", $newval . ";");

					break;
				}

				$reunionDB->setparticipant($pid, array($listoptmap[$field] => $val));

				if($field == 'priority')
				{
					$val = $val == '0' ? "No" : "Yes";
				}
			break;
		}

		return $val;
	}

	function generateJSONcalendar() {
		global $reunionDB;

		$wks = $reunionDB->getallweeks();
		$total = $reunionDB->totalparticipants();
		$output = "[";

		foreach($wks as $wk)
		{
			$people = $reunionDB->unavailablebyweek($wk->WeekID);
			$event = $wk->Event == "No Events" ? "" : str_replace("'", "\'", $wk->Event);

			if(count($people) == 0)
			{
				$ua = "[]";
			}
			else
			{
				$ua = "[ ";
				foreach($people as $pid)
				{
					$person = $reunionDB->getparticipant($pid->ParticipantID);
					$ua .= "{" . "person: '{$person->Name}', priority: '{$person->Priority}'" . "}, ";
				}
				$ua .= "]";
				$ua = str_replace("}, ];", "} ]", $ua . ";");
			}

			$wkrow = "{";
			$wkrow .= "total: '$total', date: '{$wk->Date}', event: '$event', unavailability: $ua";
			$wkrow .= "}, ";

			$output .= $wkrow;
		}
		$output .= "]";
		$output = str_replace(", ];", "]", $output . ";");
		$output = $output == "[];" ? "null" : $output;

		return $output;
	}
?>